package org.eclipseuseradmin.ui.editors;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.ColumnLayoutData;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipseuseradmin.internal.ui.editors.EditorsMessages;
import org.eclipseuseradmin.ui.utils.FormPageSupport;

public class RoleElementOverviewPage extends FormPage {

	private DataBindingContext bindingContext;

	private FormPageSupport formPageSupport;

	protected SectionPart generalInfoSection;

	public RoleElementOverviewPage(BaseRoleElementEditor editor) {
		this(editor, "overview",
				EditorsMessages.get().RoleElementOverviewPage_title);
	}

	public RoleElementOverviewPage(BaseRoleElementEditor editor, String id,
			String title) {
		super(editor, id, title);
		bindingContext = new DataBindingContext();
		formPageSupport = FormPageSupport.create(editor.getEditorSupport(),
				this, bindingContext);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {

		super.createFormContent(managedForm);

		Form form = managedForm.getForm().getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		toolkit.decorateFormHeading(form);
		managedForm.getMessageManager().setDecorationPosition(
				SWT.BOTTOM | SWT.RIGHT);
		managedForm.getMessageManager().setAutoUpdate(true);
		form.setText(getTitle());

		form.getToolBarManager().update(true); // NEW LINE
		form.setToolBarVerticalAlignment(SWT.BOTTOM); // NEW LINE

		ColumnLayout layout = new ColumnLayout();
		form.getBody().setLayout(layout);

		createSections(form.getBody());

		managedForm.getMessageManager().update();
	}

	protected void createSections(Composite parent) {

		generalInfoSection = new RoleElementGeneralInfoSection(parent, this, 0);
		ColumnLayoutData layoutData = new ColumnLayoutData(ColumnLayoutData.FILL);
		generalInfoSection.getSection().setLayoutData(layoutData);
	}

	public BaseRoleElementEditor getRoleElementEditor() {
		return (BaseRoleElementEditor) getEditor();
	}

	public DataBindingContext getBindingContext() {
		return bindingContext;
	}

	public FormPageSupport getFormPageSupport() {
		return formPageSupport;
	}
}
